package com.aier.src.action.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.eicp.ghostboy.futil.MD5Util;
import net.eicp.ghostboy.futil.StringUtil;

import com.aier.common.common.CommonSession;
import com.aier.common.common.DES;
import com.aier.src.action.BaseAction;
import com.aier.src.common.SystemFinalData;
import com.aier.src.common.SystemStatic;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckUserLoadInterceptor_app extends AbstractInterceptor {

    /**
     * 手机用户拦截器
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name="userService")
    private IUserService userService;

    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        BaseAction action = (BaseAction) arg0.getAction();
        HttpServletRequest request = action.getRequest();
        HttpSession session=request.getSession();
        if (StringUtil.isStringNull(request.getParameter("ci"))) {
            // 无渠道编号访问
            return arg0.invoke();
        }
        // 渠道解码
       String signkey = SystemStatic.des_sign_key[Integer.parseInt(request.getParameter("ci"))];
       // 解密

      String account= request.getParameter("account");
       String pwd= request.getParameter("pwd");
       // 生成密码记录key
        String key = account+pwd;
        // 查找出超时值
        Integer ltime = SystemStatic.android_user.get(key);
        if (null != ltime) {
            // 值存在就对该值重新计时
            SystemStatic.android_user.put(key,SystemStatic.android_load_out_time);
            //加入用户登陆信息
            User user = SystemStatic.android_user_account_list.get(key);
            if(user!=null){
                session.setAttribute(CommonSession.SESSION_ACCOUNT, user.getAccount());
                session.setAttribute(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                session.setAttribute(CommonSession.SESSION_ACCOUNT_NAME, user.getNickname());
            }
            return arg0.invoke();
        }
        // 查找出该用户
        User u = userService.searchUserByCondition(account, MD5Util.getMD5(pwd));
        // 判断密码是否匹配
        if (u != null){
            session.setAttribute(CommonSession.SESSION_ACCOUNT, u.getAccount());
            session.setAttribute(CommonSession.SESSION_ACCOUNT_ID, u.getUid());
            session.setAttribute(CommonSession.SESSION_ACCOUNT_NAME, u.getNickname());
            // 密码匹配新建账户维护计时器
            SystemStatic.android_user.put(key,SystemStatic.android_load_out_time);
            //加入用户登陆信息
            SystemStatic.android_user_account_list.put(key,u);
            return arg0.invoke();
        }
        
        return "nologin";
    }

    /**
     * 
     * @param param
     * @return
     */
    private Map<String, String> parseParam(String param) {
        String[] tp = param.split("&");
        Map<String, String> mk = new HashMap<String, String>();
        for (String t : tp) {
            String[] ts = t.split("=");
            if (ts.length >= 2) {
                mk.put(ts[0], ts[1]);
            } else {
                mk.put(ts[0], "");
            }
        }
        return mk;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
}